﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using RearLivestock.Common.Domains.wbx;
using RearLivestock.Common.Dtos;
using RearLivestock.Common.Infrastructure.Interface;
using RearLivestock.RBAC.ErrorCodeEnum;

namespace RearLivestock.RBAC.Api.Application.zjyApplication.Commands.Permissionses
{
    public class SearchALLRoleHandler : IRequestHandler<SearchALLRoleCommand, Result<List<Roles>>>
    {
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<SearchALLRoleHandler> _logger;

        /// <summary>
        /// 角色仓储
        /// </summary>
        private readonly IBaseRepository<Roles> _roleRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="roleRepository"></param>
        public SearchALLRoleHandler(ILogger<SearchALLRoleHandler> logger,
                                 IBaseRepository<Roles> roleRepository)
        {
            _logger = logger;
            _roleRepository = roleRepository;
        }

        /// <summary>
        /// 业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result<List<Roles>>> Handle(SearchALLRoleCommand request, CancellationToken cancellationToken)
        {
            var result = new Result<List<Roles>>() { Code = (int)CommonErrorCode.Search };
            try
            {
                var res = await (await _roleRepository.GetAll()).Where(x => x.IsDel == false).ToListAsync();
                if (res != null)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Data = res;
                    result.Msg = "获取角色信息成功";
                }

            }
            catch (Exception ex)
            {
                _logger.LogError("获取角色信息异常" + ex.Message);
            }
            return result;
        }
    }
}
